let app = getApp();
Page({
    data: {
      header:{},
      userInfo:{},
      shebeiNum:0,
      tieziNum:0,
      showLogin:false,
      phone:''
    },
    showFloat:function(){
       if (!this.data.isButler) {
        wx.navigateTo({
            url: '/pages/cshhr-s/cshhr'
        })

         return
       }

       this.setData({
        showFloat:true
       })
    },
    onLoad: function(options) {
   
    },
     //输入电话
    textPhone:function (e) {
       this.setData({
        phone:e.detail.value
      })
    },
    //提交手机号
    bindPhone:function(){
      console.log(this.data.phone)
      if (this.data.phone.length!=11) {
          wx.showToast({
          title: '请填写完整手机号',
          icon:'none',
          duration: 2000
        })
        return
      }
      var _self=this;
      wx.request({
            url:getApp().globalData.baseUrl+'/wx/yxj/appointment/add',
            method:"post",
            header: this.data.header,
            data:{
              openid:this.data.userInfo.openid,
              name:this.data.userInfo.nickName,
              phone:this.data.phone,
              content:'发布设备',
              province:this.data.userInfo.province,
              city:this.data.userInfo.city
            },
            success: function(res) {
            console.log(res)  
             _self.setData({
              showFloat:false,
              phone:''
             })
            
         }
       })
    },
    closeFloat:function(){
     this.setData({
        showFloat:false
      })
    },
    onCannel:function(){
     this.setData({
        showLogin:false
      })
    },
    onLoginSuccess:function () {
      this.setData({
        showLogin:false
      })
      this.onShow();
    },
    //帖子
    getTiezi:function () {
        var _self=this;
         wx.request({
            url:getApp().globalData.baseUrl+'/wx/articles/getList?openid='+this.data.userInfo.openid,
            method:"GET",
            header: this.data.header,
            success: function(res) {
            _self.setData({
                tieziNum:res.data.obj.total
            })
             console.log(res.data.obj.total)
           
           
         }
       })
    },
     //设备
    getSheBei:function () {
        var _self=this;
         wx.request({
            url:getApp().globalData.baseUrl+'/wx/equipment/publish/getList?openid='+this.data.userInfo.openid,
            method:"GET",
            header: this.data.header,
            success: function(res) {
            _self.setData({
                shebeiNum:res.data.obj.total
            })
             console.log(res.data.obj.total)
           
           
         }
       })
    },
    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成

    },
    onShow: function() {
       // var userInfo= wx.getStorageSync('userInfo');
       // if (!userInfo.nickName) {
       //   this.setData({
       //    showLogin:true
       //   })
       //   return
       // }
       getApp().getUserInfo().then(res=>{
      var header={
             'openid':res.openid, //公用请求头
             'content-type': 'application/json'
         }
       this.setData({
        userInfo:res,
        header:header
       })  
       // this.getTiezi();
       // this.getSheBei();
       this.getButler();
       this.getMemberInfo();
       })
    },
     getMemberInfo:function () {
         var _self=this;
         wx.request({
            url: getApp().globalData.baseUrl +'/wx/user/champion/num?openid='+this.data.userInfo.openid,
            method:"GET",
            header: {
                'openid':this.data.userInfo.openid, //公用请求头
                'content-type': 'application/json'
            },
            success: function(res) {
                console.log('**********')
             console.log(res.data.obj)
             _self.setData({
                pageObj:res.data.obj
            })
            
         }
       })

    },
    //获取严选机类型
    getButler:function(){
      var _self=this;
      wx.request({
            url:getApp().globalData.baseUrl+'/wx/yxj/butler',
            method:"get",
            header: this.data.header,
            data:{
              openid:this.data.userInfo.openid
            },
            success: function(res) {
            console.log(res)  
            if (res.data.code==200) {
               if (res.data.obj) {
                  _self.setData({
                  isButler:true
                })
                }else{
                  _self.setData({
                    isButler:false
                  })
                }
            }else{
              _self.setData({
                    isButler:false
              })
              
            }
            
         }
       })   
    },
    onHide: function() {
        // 生命周期函数--监听页面隐藏

    },
    turnPage:function (e) {
     



      this.setData({
        showFloat:false
      })
     console.log(e.currentTarget.dataset.url)
      var userInfo= wx.getStorageSync('userInfo');
       if (!userInfo.nickName) {
         this.setData({
          showLogin:true
         })
         return
       }
      if (userInfo.phone==null||userInfo.phone.length!=11) {
         this.setData({
         showToast:true
        })
        return
      }
       wx.navigateTo({
            url: e.currentTarget.dataset.url
        })
    },
    //更新用户信息
    bindGetUserInfo (e) {
    console.log(e.detail.userInfo);
    var _self=this;
     wx.request({
            url:getApp().globalData.baseUrl+'/wx/user/update',
            method:"post",
            header: this.data.header,
            data:e.detail.userInfo,
            success: function(res) {
            console.log(res)  
            _self.onShow();
         }
       })

  }, eredarClick (e) { // 达人详情
     var userInfo= wx.getStorageSync('userInfo');
       if (!userInfo.nickName) {
         this.setData({
          showLogin:true
         })
  
         return
       }
      var id=e.currentTarget.dataset.code;
     console.log(e)
     console.log(id)

      wx.navigateTo({
            url: '/pages/eredarDetail/eredarDetail?openid='+id
        })
    },
    editInfo:function () {
       console.log(123)
        wx.navigateTo({
            url: '/pages/changeInfo/changeInfo'
        })
    },
    onCannel1:function () {
     this.setData({
       showToast:false
     })
    },
    onLoginSuccess1:function () {
       this.setData({
       showToast:false
      })
    }
   

})